import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

import VuexPersist from "vuex-persist";

const vuexLocal = new VuexPersist({
  storage: window.sessionStorage,
})

export default new Vuex.Store({
  state: {
    cart_list: [],
    token: '',
  },
  mutations: {
    addCart(state, good) {
      let index = state.cart_list.findIndex(item => {
        return item.id == good.id
      })
      if (index > -1) {
        state.cart_list[index].num++
      } else {
        state.cart_list.push(good)
      }
    
    },
    login(state, data) {
      state.token = data.token
    },
    outlogin(state){
      state.token = ''
    }
  },
  actions: {

  },
  getters: {
    price(state) {
      let money = 0
     state.cart_list.filter(item=>{
       if(item.checked == true){
         return money += item.num * item.originalPrice
       }
        
      })
      return money * 100
    },
  },
  plugins: [vuexLocal.plugin],
})
